/*
 * @author : lijian
 * @creation : 2014-6-10 下午3:46:28
 * @description : 
 *	停车场该用户管理
 */

package com.action.park;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import net.sf.json.JSONObject;

import com.bean.ParkManager;
import com.bean.Parkinfo;
import com.dao.ParkManagerDao;
import com.service.ParkManagerService;
import com.service.ParkinfoService;
import com.util.GUtil;
import com.util.ValidateUtil;

/**
 * 停车场用户管理
 * <p>Title: MyParkManangerAction</p>
 * <p>Description: </p>
 * <p>Company: www.teleframe.cn</p> 
 * @author	lijian
 * @date	2014-6-11
 */
public class MyParkManagerAction extends BaseParkAction {

	ParkManagerService pmService;
	ParkManager parkManager;
	List<ParkManager> listMyPM ;//我的收费员列表
	List<ParkManager> list ;
	ParkinfoService piService;
	Parkinfo parkInfo;
	List<Parkinfo> ListPI;
	Integer  parkIds[] = null;
	Timestamp ts = new Timestamp(System.currentTimeMillis());
	SimpleDateFormat sdf = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss ");
	String date = sdf.format(ts);
	String headerTitle = null;
	Integer flag = 0;

	@Override
	public String handle() throws Exception {
		System.out.println("MyParkManagerAction handle()");
		if(id!=null && id>0)
			return this.view();
		return this.query();
	}

	/**
	 * <p>Title: query 查询停车场用户</p>
	 * <p>Description: 
	 * 		首先根据parkuser.uid查询我的停车场getMyParkArea,即获取List &lt; ParkInfo &gt;,再根据Parkinfo.id 获取 其他停车场用户。
	 * </p>
	 * @return
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		if(parkuser==null)
			parkuser = (ParkManager) session.get("parkuser");
		if(parkuser==null)
			return GUtil.LOGIN;
		list = pmService.getMyParkUsers(parkuser, parkManager);//查询我的停车场管理（管理员&收费员）用户列表
		session.put("listMyPM", list);
		return GUtil.SUCCESS;
	}

	@Override
	public String update() throws Exception {
		this.setFlag(1);
		this.setHeaderTitle("修改用户信息");
		if(parkuser==null)
			parkuser = (ParkManager) session.get("parkuser");
		if(parkuser==null)
			return GUtil.LOGIN;

		if(parkManager==null){
			parkManager = pmService.get(Long.valueOf(id));//查询用户信息和此用户的管理的停车场
			ListPI = piService.getMyParkArea(parkuser);//查询我的停车场列表
			for(Parkinfo pilist : ListPI){
				for(Parkinfo pi :parkManager.getList()){
					log.info("pilist.getPId()="+pilist.getPId()+" pi.getPId()="+pi.getPId());
					if( (pilist.getPId()-pi.getPId())==0){
						pilist.setChecked(true); //此用户的管理的停车场的IDs
						break;
					}else
						pilist.setChecked(false);
				}
			}
			session.put("parkManager.id", id);
			//	System.out.println(JSONObject.fromObject(parkManager));
			return GUtil.EDIT;
		}

		id = (Integer) session.get("parkManager.id");
		parkManager.setUserId( Long.valueOf(id) );
		if(parkManager.getUserId()==null){
			log.error(this.getClass()+".update() id==null");
			this.setMsg("内部错误");
			return GUtil.ERROR;
		}

		//用户名、密码、真实姓名等后台校验
		if(!this.validate(parkManager)){
			return GUtil.EDIT;
		}

		//检查更新的此用户是否是属于自己管辖分的用户
		list = (List<ParkManager>) session.get("listMyPM");
		if(list==null)
			list = pmService.getMyParkUsers(parkuser);//查询我的停车场管理（管理员&收费员）用户列表
		if(list==null || list.size()<1){
			this.setMsg("您没有任何收费员或管理员");
			log.error(this.getClass()+"update() ip="+ip+" userid="+user.getId()+" 该用户没有任何收费员或管理员，但是在尝试修改用户信息");
			return GUtil.ERROR;
		}
		boolean bOk = false;
		for(ParkManager pm : list ){
			if(pm.getUserId()==parkManager.getUserId()){
				bOk = true;
				break;
			}
		}
		if(bOk==false){
			this.setLogmsg("停车场用户[parkuser.userCode="+parkuser.getUserName()+"]正在尝试修改非自己管辖范围的用户信息[id="+parkManager.getUserId()+",userCode="+parkManager.getUserName()+"]");
			return GUtil.ERROR;
		}
		//更新用户信息
		parkManager.setSuperiorOfficer(user.getId());//上级领导
		String ret = pmService.update(parkManager, parkIds);
		System.out.println(parkManager.getRealName()+"--------------------------------");
		if(ret.equals(GUtil.SUCCESS)){
			this.setMsg("修改成功");
			return this.query();
		}else{
			this.setMsg("修改失败");
			return GUtil.EDIT;
		}
	}

	@Override
	public String add() throws Exception {
		flag=0;
		this.setHeaderTitle("添加用户");
		if(parkuser==null)
			parkuser = (ParkManager) session.get("parkuser");
		if(parkuser==null)
			return GUtil.LOGIN;

		ListPI = piService.getMyParkArea(parkuser);//查询我的停车场列表
		if(parkManager==null){
			return GUtil.EDIT;
		}

		//用户名、密码、真实姓名等后台校验
		if(!this.validate(parkManager)){
			return GUtil.EDIT;
		}

		list =  pmService.findByProperty(ParkManagerDao.USER_NAME, parkManager.getUserName());
		if(list!=null && list.size()>0){
			this.setMsg("抱歉，此用户已存在");
			return GUtil.EDIT;
		}

		String ret = GUtil.SUCCESS;
		parkManager.setSuperiorOfficer(user.getId());//上级领导
		ret = pmService.add(parkManager, parkIds);
		if(ret.equals(GUtil.SUCCESS)){
			this.setMsg("添加用户成功");
			parkManager = null;
		}else
			this.setMsg("添加用户失败");
		return GUtil.EDIT;
	}

	@Override
	public String delete() throws Exception {
		session.put("userinfo", pmService.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("userinfo");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(pmService.delete(Long.valueOf(id),-1).equals(GUtil.SUCCESS)){
				this.setMsg("删除成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "成功删除");
			}else{
				this.setMsg("删除失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][删除用户:"
						+ or.getRealName() + "失败");
			}
		}
		return this.query() ;
	}
	/*
	 * 锁定
	 */

	public String locking() throws Exception {
		session.put("manager", pmService.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("manager");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(pmService.lock(Long.valueOf(id),0).equals(GUtil.SUCCESS)){
				this.setMsg("锁定成功");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "已锁定");
			}else{
				this.setMsg("锁定失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "锁定失败");
			}
		}
		return this.query() ;
	}
	/*
	 * 解锁
	 */
	public String delocking() throws Exception {
		session.put("manager", pmService.get(Long.valueOf(id)));
		ParkManager or = (ParkManager) session.get("manager");
		if(id==null || id<1)
			return GUtil.ERROR;
		else{
			if(pmService.lock(Long.valueOf(id),1).equals(GUtil.SUCCESS)){
				this.setMsg("成功解锁");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "已成功解锁");
			}else{
				this.setMsg("解锁失败");
				this.setLogmsg("[" + date + "][后台管理员:" + user.getUserCode() + ","
						+ user.getUserName() + "][把用户:"
						+ or.getRealName() + "已成功解锁");
			}
		}
		return this.query() ;
	}
	@Override
	public String view() throws Exception {
		if(id==null || id<1)
			return GUtil.ERROR;
		parkManager =  pmService.get(Long.valueOf(id));
		session.put("park", parkManager);
		return GUtil.VIEW;
	}

	@Override
	public boolean validate(Object obj) throws Exception {
		boolean bok = true;
		if(method.equals("add")){
			if(ValidateUtil.chekcUserName(parkManager.getUserName())==false){
				this.addFieldError("manager.userName", "用户名不符合规范");
				bok &= false ;
			}
		}
		//if method==add 校验，else if parkManager.userPasswd!="" 校验
		if(method.equals("add") || (parkManager.getUserPasswd()!=null && !parkManager.getUserPasswd().trim().isEmpty()) ){
			if(ValidateUtil.chekcPassword(parkManager.getUserPasswd())==false){
				this.addFieldError("manager.userPassowrd", "密码不符合规范");
				bok &= false ;
			}
		}
		if(ValidateUtil.chekcRealName(parkManager.getRealName())==false){
			this.addFieldError("manager.realName", "真实姓名不符合规范");
			bok &= false ;
		}

		///***   添加其他的校验
		//  .....

		return bok;
	}

	//getter & setter

	public Integer getFlag() {
		return flag;
	}

	public void setFlag(Integer flag) {
		this.flag = flag;
	}

	public String getHeaderTitle() {
		return headerTitle;
	}

	public void setHeaderTitle(String headerTitle) {
		this.headerTitle = headerTitle;
	}


	public ParkinfoService getPiService() {
		return piService;
	}

	public void setPiService(ParkinfoService piService) {
		this.piService = piService;
	}

	public ParkManagerService getPmService() {
		return pmService;
	}

	public void setPmService(ParkManagerService pmService) {
		this.pmService = pmService;
	}

	public Parkinfo getParkInfo() {
		return parkInfo;
	}

	public void setParkInfo(Parkinfo parkInfo) {
		this.parkInfo = parkInfo;
	}

	public ParkManager getParkManager() {
		return parkManager;
	}

	public void setParkManager(ParkManager parkManager) {
		this.parkManager = parkManager;
	}

	public List<ParkManager> getList() {
		return list;
	}

	public void setList(List<ParkManager> list) {
		this.list = list;
	}


	public List<Parkinfo> getListPI() {
		return ListPI;
	}

	public void setListPI(List<Parkinfo> listPI) {
		ListPI = listPI;
	}

	public Integer[] getParkIds() {
		return parkIds;
	}

	public void setParkIds(Integer[] parkIds) {
		this.parkIds = parkIds;
	}

	public List<ParkManager> getListMyPM() {
		return listMyPM;
	}

	public void setListMyPM(List<ParkManager> listMyPM) {
		this.listMyPM = listMyPM;
	}



}
